/**
 * 
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2011-6-23 上午11:16:14
 * @version V1.0
 *
 */
package com.starbaba.core.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.starbaba.core.utils.config.ConfigFileFactory;

/**
 * 实现DES加密解密
 * 
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2011-6-23 上午11:16:14
 * @version V1.0
 * 
 */
public class DESUtil {
	private static final Log log = LogFactory.getLog(DESUtil.class);
	
	private static final String DES_DEFAULT_ENCODING = "UTF-8";	
	/**
	 * 8位密匙，可从配置文件读取
	 */
	private static String DES_DEFAULT_KEY = "00000000";
	
	static {
		DES_DEFAULT_KEY = ConfigFileFactory.getInstance().get("Constant").getValue("des.key");
	}
	/**
	 *  解密
	 *  
	 * @param message 密文
	 * @param key 8位密钥，为null时，使用密匙DES_DEFAULT_KEY
	 * @return String 解密后的数据
	 * @throws Exception
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-6-23 上午11:35:24
	 * @version V1.0
	 *
	 */
	public static String decrypt(String message, String key) throws Exception {
		if(key == null) {
			key = DES_DEFAULT_KEY;
		}
		
		byte[] bytesrc = convertHexString(message);
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(DES_DEFAULT_ENCODING));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		IvParameterSpec iv = new IvParameterSpec(key.getBytes(DES_DEFAULT_ENCODING));

		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
		byte[] retByte = cipher.doFinal(bytesrc);
		return URLDecoder.decode(new String(retByte), DES_DEFAULT_ENCODING);
	}

	/**
	 *  加密
	 * @param message 待加密的数据
	 * @param key 8位密钥，为null时，使用密匙DES_DEFAULT_KEY
	 * @return String 加密后的数据
	 * @throws Exception
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-6-23 下午02:23:52
	 * @version V1.0
	 *
	 */
	public static String encrypt(String message, String key) throws Exception {
		if(key == null) {
			key = DES_DEFAULT_KEY;
		}
		
		message = URLEncoder.encode(message, DES_DEFAULT_ENCODING);
		
		DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(DES_DEFAULT_ENCODING));

		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
		
		IvParameterSpec iv = new IvParameterSpec(key.getBytes(DES_DEFAULT_ENCODING));
		
		Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
		return toHexString(cipher.doFinal(message.getBytes(DES_DEFAULT_ENCODING)));
	}
		
	/**
	 *  转换为16进制字节
	 *  
	 * @param ss 源字符串
	 * @return byte[] 16进制字节
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-6-23 下午02:24:40
	 * @version V1.0
	 *
	 */
	public static byte[] convertHexString(String ss) {
		byte digest[] = new byte[ss.length() / 2];
		for (int i = 0; i < digest.length; i++) {
			String byteString = ss.substring(2 * i, 2 * i + 2);
			int byteValue = Integer.parseInt(byteString, 16);
			digest[i] = (byte) byteValue;
		}

		return digest;
	}
	
	/**
	 *  16进制字节转换为字符串
	 *  
	 * @param b 16进制字节
	 * @return String 字符串
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-6-23 下午02:25:06
	 * @version V1.0
	 *
	 */
	public static String toHexString(byte b[]) {
		StringBuffer hexString = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			String plainText = Integer.toHexString(0xff & b[i]);
			if (plainText.length() < 2)
				plainText = "0" + plainText;
			hexString.append(plainText);
		}

		return hexString.toString();
	}
	
	/**
	 *  测试
	 *  
	 * @param args
	 * @throws Exception
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2011-6-23 下午02:25:44
	 * @version V1.0
	 *
	 */
	public static void main(String[] args) throws Exception {
		String key = null;
		log.info("使用默认密匙");
		String value = "test1234 我测试—— TEST_例子";
		log.info("加密前的数据:" + value);
		String a = encrypt(value, key);

		log.info("加密后的数据:" + a);
		String b = decrypt(a, key);
		log.info("解密后的数据:" + b);
		
		log.info("-------------------------------------------");
		key = "88888888";
		log.info("使用加密key:" + key);
		log.info("加密前的数据:" + value);
		a = encrypt(value, key);

		log.info("加密后的数据:" + a);
		b = decrypt(a, key);
		log.info("解密后的数据:" + b);
	}
}
